package com.topcheer.provider.rbac.rest;

import com.github.pagehelper.PageInfo;
import com.topcheer.common.auth.user.JwtUser;
import com.topcheer.common.base.util.JsonMapper;
import com.topcheer.common.base.vo.BaseResponse;
import com.topcheer.provider.rbac.entity.User;
import com.topcheer.provider.rbac.service.RoleService;
import com.topcheer.provider.rbac.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(description = "用户信息")
public class UserController{

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @PostMapping("")
    @ApiOperation(value="新增用户", notes="新增用户", produces="application/json")
    public BaseResponse<User> insert(@RequestBody User entity){
        userService.insert(entity);
        return new BaseResponse<User>().code(HttpStatus.OK.value()).msg(HttpStatus.OK.getReasonPhrase());
    }

    @GetMapping("/{id}")
    @ApiOperation(value="根据id获取用户", notes="根据id获取用户", produces="application/json")
    public BaseResponse<User> selectOne(@PathVariable("id") Long id){
        Object o = userService.selectOne(id);
        return new BaseResponse<User>().data((User) o);
    }

    @GetMapping("/page")
    @ApiOperation(value="获取用户分页信息", notes="获取用户分页信息", produces="application/json")
    public BaseResponse<PageInfo> page(String page){
        JsonMapper jsonMapper = JsonMapper.defaultMapper();
        Map map = jsonMapper.fromJson(page, Map.class);
        PageInfo pageInfo = userService.selectPage(map);
        return new BaseResponse<PageInfo>().data(pageInfo);
    }

    @PutMapping("")
    @ApiOperation(value="更新用户", notes="更新用户", produces="application/json")
    public BaseResponse<User> update(@RequestBody User entity){
        userService.update(entity);
        return new BaseResponse<User>();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value="删除用户", notes="删除用户", produces="application/json")
    public BaseResponse<User> delete(@PathVariable("id") Long id){
        userService.deleteById(id);
        return new BaseResponse<User>();
    }

    @GetMapping("/all")
    @ApiOperation(value="获取所有用户", notes="获取所有用户", produces="application/json")
    public BaseResponse<List> selectAll(){
        List list = userService.selectAll();
        return new BaseResponse<List>().data(list);
    }

    @GetMapping("/baseUser/{loginName}")
    @ApiOperation(value="根据登陆名获取用户信息", notes="根据登陆名获取用户信息", produces="application/json")
    public BaseResponse<JwtUser> getJwtUser(@PathVariable("loginName") String loginName) {
        User user = new User();
        user.setLoginName(loginName);
        List<User> userList = userService.select(user);
        if (!CollectionUtils.isEmpty(userList)) {
            User info = userList.get(0);
//            List<Role> roleList = roleService.selectRoleByUserId(info.getId());
//            List<SimpleGrantedAuthority> authorities = roleService.selectRoleByUserId(info.getId()).stream().map(Role::getCode).map(SimpleGrantedAuthority::new).collect(Collectors.toList());
            JwtUser jwtUser = new JwtUser(loginName, info.getPassword(), info.getId(), null);
            jwtUser.setOrgId(info.getOrgId());
            return new BaseResponse<JwtUser>().data(jwtUser);
        }
        return new BaseResponse<JwtUser>();
    }

}
